﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class PancakeDisController : BaseController<preselldiscount>
    {
        //
        // GET: /PancakeDis/  煎饼优惠活动
        public override BLL.BaseBll<preselldiscount> bll
        {
            get
            {
                return new PancakeDisBll();
            }
            
        }
        StoreBll storeBll = new StoreBll();
        public class discountModel
        {
            //discountId   discountMoney   discountName discountType storeName   startTime overTime
            public int discountId;
            public string  discountMoney;
            public string discountName;
            public string storeName;
            public string discountType;
            public string  startTime;
            public string  overTime;
            public string status;
        }
        public class discountOneModel
        {
            //discountId   discountMoney   discountName discountType storeName   startTime overTime
            public int discountId;
            public string discountMoney;
            public string discountName;
            public string storeName;
            public int? storeID;
            public string discountType;
            public string startTime;
            public string overTime;
            public string status;
        }
        List<discountOneModel> disOneList = new List<discountOneModel>();
        List<discountModel> disList = new List<discountModel>();

        public  ActionResult Get2(int page, int rows, preselldiscount model)
        {   
            int count;
            var list = bll.Search(model, x => true, true, x => x.discountId, rows, page, out count).Select(x => new { x.discountId, status=x.status==1?"有效":"无效", x.discountMoney, x.discountName, x.discountStoreid, x.discountType, x.startTime, x.overTime }).ToList();
            foreach (var item in list)
            {
                int code = Convert.ToInt32(item.discountStoreid);
                string storeName = storeBll.Search(x => x.storeid == code).Select(x => new { x.storeName }).FirstOrDefault().storeName;
                discountModel modelArea = new discountModel();
                
                modelArea.discountId = item.discountId;
                modelArea.discountMoney = item.discountMoney;
                modelArea.discountName = item.discountName;
                modelArea.storeName = storeName;
                modelArea.discountType = item.discountType;
                modelArea.startTime =Convert.ToDateTime( item.startTime).ToString("yyyy-MM-dd");
                modelArea.overTime = Convert.ToDateTime(item.overTime).ToString("yyyy-MM-dd");
                modelArea.status = item.status;
                disList.Add(modelArea);
            }
            return Json(new { total = count, rows = disList });
        }

        public ActionResult GetStore()
        {
            return Json(storeBll.Search(x => x.storeStatus == 1).Select(x => new { StoreID = x.storeid, StoreName = x.storeName }).ToList());

        }

        public ActionResult Save2(preselldiscount model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;

            //模板设计器
            if (model.discountName != "" && model.discountMoney != "" && model.discountName != null && model.discountMoney != null)
            {
           
                    model.status = 1;

                    if (bll.Add(model) > 0)
                    {
                        msg = "添加成功";
                        jieguo = 1;
                    }
                    else
                    {
                        jieguo = 1;
                        msg = "添加失败";

                    }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";

            }
            return Json(new { jieguo = jieguo, state = result, msg = msg });
        }


        //获取选中的一条数据   
        public ActionResult GetOnlyOne(long id)
        {
           
            var list = bll.Search(x => x.discountId == id).ToList().Select(x => new { x.discountId, x.discountMoney, x.discountName, x.discountType , x.overTime , x.startTime,x.discountStoreid }).ToList();
            foreach (var item in list)
            {

                string storeName = storeBll.Search(x => x.storeid == item.discountStoreid).Select(x => new { StoreID = x.storeid, StoreName = x.storeName }).ToList().FirstOrDefault().StoreName;
                discountOneModel model = new discountOneModel();
                model.discountId = item.discountId;
                model.discountMoney = item.discountMoney;
                model.discountName = item.discountName;
                model.discountType = item.discountType;
                model.storeID = item.discountStoreid;
                model.storeName = storeName;
                model.overTime = Convert.ToDateTime(item.overTime).ToString("yyyy-MM-dd");
                model.startTime = Convert.ToDateTime(item.startTime).ToShortDateString();
                disOneList.Add(model);
     
            }
            return Json(disOneList.ToList()[0], JsonRequestBehavior.AllowGet);
        }

        public ActionResult Modify2(preselldiscount model)
        {
            string msg = "修改失败";
            string state = "false";

            model.status = 1;

            if (model.discountName != "" && model.discountName != null && model.discountMoney != "" && model.discountMoney != null)
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                state = "false";
            }

            return Json(new { state = state, msg = msg });
        }

        //撤销活动  修改状态为0
        public ActionResult ModifyState(int id)
        {
            string msg = "撤销失败";
            string state = "false";

            var oneList = bll.Search(x => x.discountId == id).Select(x => new { x.discountId, x.discountMoney, x.discountName, x.discountType, x.overTime, x.startTime, x.discountStoreid }).ToList();

            preselldiscount model = new preselldiscount();
            model.discountId = oneList.FirstOrDefault().discountId;
            model.discountMoney = oneList.FirstOrDefault().discountMoney;
            model.discountName = oneList.FirstOrDefault().discountName;
            model.discountStoreid = oneList.FirstOrDefault().discountStoreid;
            model.discountType = oneList.FirstOrDefault().discountType;
            model.overTime = oneList.FirstOrDefault().overTime;
            model.startTime = oneList.FirstOrDefault().startTime;
            model.status = 0;

            if (bll.Modify(model) > 0)
            {
                msg = "撤销成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }
    }
}
